<?php
#### NAVIGATION MANAGEMENT CONTROLLER ############################################
class NavigationController {

	var $feedback;

	#### CONSTRUCTOR ###########################################################
	function NavigationController() {
		Base::getThis();

		// Set the page title
		$this->page_title = _('Skeleton Module');
		if (url(3)) $this->page_title .= ' '.ucwords(url(3));

		// Get variables
		variable($this->id, 'id', 'get', url(4));

		// Make the model ready for use
		$this->model = $this->load->model('navigation');

		// The header is to be included module wide
		$this->buildHeader();

		// Perform default function
		invoke_method($this, url(3));
	}

	#### BUILD HEADER ##########################################################
	function buildHeader() {

		// To Exlude this header from being shown on a function set the functions output as
		// "$this->output =" rather than appending to $this->output with "$this->output .="

		// Build the links array
		$links[] = array(_('Config'), url(1, 2).'/config', $this->icon['config']);
		$links[] = array(_('Manage Navigation'), url(1, 2), $this->icon['navigation_manage']);
		$links[] = array(_('Add a Navigation Item'), url(1, 2).'/add', $this->icon['navigation_add']);

		// Include categories library, configure categories and return peformed action such as manage/edit/add
		$mod_head = $this->load->library('module_header');
		$mod_head->setName(_('Navigation'));
		$mod_head->setIcon($this->icon['navigation']);
		$mod_head->setLinks($links);
		$this->output = $mod_head->output();
	}

	#### CONFIG ############################################################
	function config() {

		// Prepopulate the form fields
		if (!$this->submit) $this->post = $this->common;

		// Build the config form
		$form = $this->load->library('form');
		$form->open();

		$form->description('navigation_separator_text', _('Separating text unless icon is used'));
		$form->description('navigation_separator_icon', _('Use a separator icon instead of text'));

		$form->text(_('Seperator Text'), 'navigation_separator_text', $this->post['navigation_separator_text'], NULL, 2);
		$form->checkbox(_('Use Seperator Icon'), 'navigation_separator_icon', $this->post['navigation_separator_icon']);
		$form->button('submit', 'Update');
		$form->close();

		// Include config library, configure config
		$mod_conf = $this->load->library('module_config');
		$mod_conf->setName(_('Navigation'));
		$mod_conf->setForm($form->output());
		$this->feedback = invoke_method($mod_conf, url(4), 'buildConfigForm');

		// Output config form or return to main function if form has been submitted
		if (!$this->submit) $this->output .= $this->feedback;
		else return $this->main();
	}

	#### MAIN ##################################################################
	function main() {

		// Get downloads from database
		$items = $this->model->getItemsByPosition();

		$tbl = $this->load->library('table');
		$tbl->open('table');
		$tbl->tr(
			$tbl->th(_('Name')).
			$tbl->th(_('Visible To'), 160, TRUE).
			$tbl->th(_('Actions'), 80, TRUE)
			, FALSE, 'nodrag nodrop');

		if ($items) foreach ($items as $item) {

			$table_name = $this->icon['link'].' '.$item['name'];
			$table_visible_to = get_allowed_to($item['visible_to']);

			$table_actions = '<a href="'.url(1, 2).'/toggle-status/'.$item['id'].'" title="'._('Toggle Status').'">'.
				($item['disabled'] ? $this->icon['disabled'] : $this->icon['enabled']).'</a> ';
			$table_actions .= '<a href="'.url(1, 2).'/edit/'.$item['id'].'" title="'._('Edit').'">'.$this->icon['edit'].'</a> ';
			$table_actions .= '<a href="'.url(1, 2).'/delete/'.$item['id'].'" title="'._('Delete').'">'.$this->icon['delete'].'</a>';

			$tbl->tr(
				$tbl->td($table_name).
				$tbl->td($table_visible_to, TRUE).
				$tbl->td($table_actions, TRUE)
				, $item['id']);
		}

		// Add an item message/link
		$tbl->tr($tbl->td($this->icon['save'].' <a href="'.url(1, 2).'" id="save_positions">'._('Save Positions').'</a> '.
			$this->icon['separator'].' '.
			$this->icon['navigation_add'].' <a href="'.url(1, 2).'/add">'._('Add a Navigation Item').'</a>', TRUE, 3), FALSE, 'footer nodrag nodrop');
		$tbl->close();

		$javascript = '<div id="table_debug"></div>
		<script language="JavaScript" src="media/javascript/jquery.tablednd.js" type="text/javascript"></script>
		<script type="text/javascript">
			$(document).ready(function() {
			    // Initialise the table
			    $("#table").tableDnD();

       			$("#save_positions").click(function(table, row) {
       				table.preventDefault();
	            	$("#table_debug").load("'.url(1, 2).'/update-position", $("#table").tableDnDSerialize());
	       		});
			});
		</script>';

		if (url('search', 'positions-updated')) $this->feedback = notice(_('The positions have been saved.'));

		// Set output
		$title = $this->icon['navigation_manage'].' '._('Manage Navigation');
		$body = _('To reposition the navigation links simply drag and drop them into
			place and click on the '.$this->icon['save'].' <b>Save Positions</b> link below').'<br /><br />';
		$body .= $javascript.
			$this->feedback.
			$tbl->output();
		$this->output .= box($title, $body);
	}

	#### ADD ###################################################################
	function add() {

		// Process form submission
		if ($this->submit && !$this->feedback) {
			// Add item to database
			$this->model->insertItem();

			$this->feedback = notice(_('Your item has been added.'));
			return $this->main();
		}

		$title = $this->icon['navigation_add'].' '._('Add a Link');
		$body = $this->buildForm('add');
		$this->output .= box($title, $this->feedback.$body);
	}

	#### EDIT ##################################################################
	function edit() {

		// Process form submission
		if ($this->submit) {
			// Update item in database
			$this->model->updateItem($this->id);

			$this->feedback = notice(_('Your item has been edited.'));
			return $this->main();
		}

		// Get model from database
		$this->post = $this->model->getItemByID($this->id);

		$title = $this->icon['edit'].' '._('Edit Item');
		$body = $this->buildForm();
		$this->output .= box($title, $this->feedback.$body);
	}

	#### DELETE ################################################################
	function delete() {

		if ($this->confirm) {
			// Delete item from database
			$this->model->deleteItem($this->id);

			$this->feedback = notice(_('Your item has been deleted.'));
			return $this->main();
		}

		// Get item from database
		$item = $this->model->getItemByID($this->id);

		// Return confirmation message
		$links[] = array(url(1, 3).'/'.$this->id.'/confirm', $this->icon['yes'].' '._('Yes'));
		$links[] = array(url(1, 2), $this->icon['no'].' '._('No'));
		$this->feedback = notice(_('Are you sure you wish to delete').' <b>'.$item['name'].'</b>?', 'warning', $links);
		return $this->main();
	}

	#### TOGGLE STATUS #########################################################
	function toggleStatus() {

		$item = $this->model->getItemByID($this->id);

		// Toggle item in database
		$this->model->updateItemStatus($this->id);
		$this->feedback = notice(_('Your item has been').': <b>'.
			($item['disabled'] ? _('Enabled') : _('Disabled')).'</b>.',
			'info');
		return $this->main();
	}

	#### UPDATE POSITION #######################################################
	function updatePosition() {
		variable($request, 'table', 'request');

		foreach($request as $key => $value) {
			if ($value) $this->model->updateItemPosition($key, $value);
		}

		echo '<script type="text/javascript">
			window.location = "'.url(1, 2).'/positions-updated";
		</script>';

		// Stop futher code from executing
		die();
	}

	#### BUILD FORM ############################################################
	function buildForm() {

		$form = $this->load->library('form');
		$this->pages = $this->load->model('pages');

		$form->description('page', 'Selecting a page will ignore the URL');

		$form->validate('name');

		$form->open();
		$form->text(_('Name').'*', 'name', $this->post['name']);
		$form->text(_('URL'), 'url', $this->post['url']);
		$form->select(_('Page'), 'page_id', $this->pages->getPagesForForm());
		$form->roles(_('Visible To'), 'visible_to', $this->post['visible_to']);
		$form->checkbox(_('Disabled'), 'disabled', $this->post['disabled']);
		$form->button('submit', (url('search', 'add') ? _('Add') : _('Edit')));
		$form->close();

		return $form->output();
	}

}
?>